package com.itheima.xx.service;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/10/23 17:18
 */
public class Login {
    public void userLogin(){

        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("info.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = (String) properties.get("url");
        String user = (String) properties.get("user");
        String password = (String) properties.get("password");
        Scanner scan = null;

        try (
                Connection conn = DriverManager.getConnection(url, user, password);
                Statement stmt = conn.createStatement();
        ) {
            scan = new Scanner(System.in);

            System.out.println("请输入登录名：");
            String inputName = scan.nextLine();
            System.out.println("请输入登录密码：");
            String inputPwd = scan.nextLine();

            String sql = "select id,username,password pwd from user where username='" + inputName + "' and password='" + inputPwd + "'";
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()){
                String username = rs.getString("username");
                System.out.println("恭喜您，亲，登录成功，欢迎光临我的小店，你的用户名是"+username);
            }else{
                System.out.println("用户名或者密码错误");
            }

            scan.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
